﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OrderEntryManagement.Modules.OrderEntry.Views;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Events;
using OrderEntryManagement.Infrastructure;
using Microsoft.Practices.Composite.Presentation.Events;

namespace OrderEntryManagement.Modules.OrderEntry
{
    public class OrderEntryController : IOrderEntryController
    {
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;
        private readonly IRegion topRightRegion;
        private readonly IRegion topLeftRegion;

        public OrderEntryController(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;

            this.topRightRegion = this.regionManager.Regions[RegionNames.TopRightRegion];
            this.topLeftRegion = this.regionManager.Regions[RegionNames.TopLeftRegion];

            eventAggregator.GetEvent<GenerateOrderEvent>().Subscribe(GenerateOrder, ThreadOption.UIThread);
        }


        public void GenerateOrder(string orderId)
        {
            OrderEntryView orderEntryView = new OrderEntryView();
            topRightRegion.Add(orderEntryView);
            topRightRegion.Activate(orderEntryView);
        }

        public void Run()
        {
            TrackOrdersView trackOrdersView = new TrackOrdersView();
            TrackOrdersViewModel trackOrdersViewModel = new TrackOrdersViewModel(this);
            trackOrdersView.DataContext = trackOrdersViewModel;

            topLeftRegion.Add(trackOrdersView);
            topLeftRegion.Activate(trackOrdersView);

        }
    }
}
